package com.tiancheng.trade.settlement.service.settlement.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.settlement.bo.ProductBO;
import com.tiancheng.trade.settlement.feign.MerchantPaymentChannelService;
import com.tiancheng.trade.settlement.mapper.settlement.FlowSubOrderMapper;
import com.tiancheng.trade.settlement.mapper.settlement.SettlementBillDataMapper;
import com.tiancheng.trade.settlement.mapper.settlement.SettlementBillResultMapper;
import com.tiancheng.trade.settlement.model.settlement.FlowSubOrder;
import com.tiancheng.trade.settlement.service.settlement.IFlowSubOrderService;
import com.tiancheng.trade.settlement.util.JacksonJsonUtil;
import com.tiancheng.trade.settlement.vo.settlement.api.FlowSubOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 此类为[代码工厂]自动生成
 *
 * @Desc 流水子订单表
 * @Time 2024-05-28 18:24
 * @GeneratedByCodeFactory
 */
@Slf4j
@Service
public class FlowSubOrderService implements IFlowSubOrderService {
    @Autowired
    private FlowSubOrderMapper flowSubOrderMapper;
    @Autowired
    private MerchantPaymentChannelService merchantPaymentChannelService;
    @Autowired
    private ObjectMapper objectMapper;
    @Resource
    private JacksonJsonUtil jacksonJsonUtil;
    @Autowired
    private SettlementBillResultMapper settlementBillResultMapper;
    @Autowired
    private SettlementBillDataMapper settlementBillDataMapper;

    @Override
    public int save(List<FlowSubOrderVO> subOrders) {
        if (subOrders == null || subOrders.isEmpty()) {
            log.info("subOrders为空.... {}");
            return 0;
        }
        log.info("推送的子订单条数为.... {}", subOrders.size());
        List<String> productCodes = new ArrayList<>();
        for (FlowSubOrderVO vo : subOrders) {
            if (!productCodes.contains(vo.getProductCode())) {
                //现在订单中心给过来的产品编号都是业务侧的，需要转换
                productCodes.add(vo.getProductCode());
            }
            vo.setProductCodeBusiness(vo.getProductCode());
        }
        log.info("推送的子订单flow_id为.... {}", subOrders.get(0).getFlowId());
        String productCodeStr = String.join(",", productCodes);
        Result<List<ProductBO>> productBOList = merchantPaymentChannelService.getProductByBussessProductCode(productCodeStr);
        List<ProductBO> productList = null;
        if (productBOList.getRet() == 0) {
            productList = productBOList.getData();
        }
        if (null != productList && !productList.isEmpty()) {
            for (FlowSubOrderVO vo : subOrders) {
                for (ProductBO bo : productList) {
                    if (vo.getProductCode().equals(bo.getProductCodeBusiness())) {
                        vo.setProductCode(bo.getProductCode());
                        vo.setProductCodeBusiness(bo.getProductCodeBusiness());
                        break;
                    }
                }
            }
        }
        List<FlowSubOrder> subs = null;
        try {
            subs = objectMapper.readValue(jacksonJsonUtil.toJson(subOrders), new TypeReference<List<FlowSubOrder>>() {
            });
            log.info("转换完子订单集合{}");
        } catch (Exception e) {
            log.info("转换子订单出现异常.... {}", e.getMessage());
            return 0;
        }
        log.info("保存的子订单flow_id为.... {}", subOrders.get(0).getFlowId());
        int count = flowSubOrderMapper.saveBatch(subs);
        log.info("保存成功的子订单条数为.... {}", count);
        return count;
    }

    @Override
    public int updateSubOrderProduct(ProductBO bo) {
        //修改流水的商品编号
        int count1 = flowSubOrderMapper.updateSubOrderProduct(bo);
        //修改账单的商品编号
        int count2 = settlementBillDataMapper.updateBillDataProduct(bo);
        //修改结果的商品编号
        int count3 = settlementBillResultMapper.updateBillResultProduct(bo);
        return count1 + count2 + count3;
    }
}